package com.bite.bite_forum.controller;
import com.bite.bite_forum.common.AppConfig;
import com.bite.bite_forum.common.AppResult;
import com.bite.bite_forum.common.ResultCode;
import com.bite.bite_forum.exception.ApplicationException;
import com.bite.bite_forum.model.Article;
import com.bite.bite_forum.model.Board;
import com.bite.bite_forum.model.User;
import com.bite.bite_forum.services.IArticleService;
import com.bite.bite_forum.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "文章接口")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    IArticleService articleService;
    @Resource
    IBoardService boardService;

    @ApiOperation("新增文章")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
            @ApiParam("板块id") @RequestParam("boardId") @NonNull Long boardId,
                            @ApiParam("标题") @RequestParam("title") @NonNull String title,
                            @ApiParam("正文") @RequestParam("content") @NonNull String content){
        //用户是否被禁言
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            log.info(ResultCode.FAILED_USER_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        //板块是否存在
        Board board = boardService.selectById(boardId);
        if(board.getState() == 1 || board.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_BOARD_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_BANNED));
        }
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setUserId(user.getId());
        article.setBoardId(boardId);
        articleService.create(article);
        return AppResult.success();
    }

    @ApiOperation("获取帖子列表")
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId(@ApiParam("boardId")
                                             @RequestParam(value = "boardId", required = false) Long boardId){
        List<Article> articles = null;
        if(boardId == null){
            articles  = articleService.selectAll();
        }else {
            articles = articleService.selectAllByBoardId(boardId);
        }
            if(articles == null){
                articles = new ArrayList<>();
            }
            return AppResult.success(articles);
    }

    @ApiOperation("帖子详细信息")
    @GetMapping("/details")
    public AppResult<Article> getDetails(HttpServletRequest request,
            @ApiParam("帖子id") @RequestParam(value = "id") @NonNull Long id){
        Article article = articleService.selectDetailById(id);
        if(article == null){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //获取登录用户信息
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getId() == article.getUserId()){
            article.setOwn(true);
        }
        return AppResult.success(article);
    }

    @ApiOperation("编辑帖子")
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,
                            @ApiParam("帖子id") @RequestParam("id") @NonNull Long id,
                            @ApiParam("标题") @RequestParam("title") @NonNull String title,
                            @ApiParam("正文") @RequestParam("content") @NonNull String content){
        //判断当前用户是否是作者
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        Article article = articleService.selectById(id);
        if(article.getUserId() != user.getId()){
            log.info(ResultCode.FAILED_UNAUTHORIZED.getMessage());
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        //用户是否被禁言
        if(user.getState() == 1){
            log.warn(ResultCode.FAILED_USER_BANNED.getMessage());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        //帖子是否能被修改
        if(article.getState() == 1 || article.getDeleteState() == 1){
            log.info(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        articleService.modify(id, title, content);
        return AppResult.success();
    }

    @ApiOperation("点赞帖子")
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request, Long id){
        //判断用户是否被禁言
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            log.info(ResultCode.FAILED_USER_BANNED.getMessage());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        articleService.thumbsUpById(id);
        return AppResult.success();
    }

    @ApiOperation("删除帖子")
    @PostMapping("/delete")
    public AppResult deleteById(HttpServletRequest  request,
                               @ApiParam("帖子id") @RequestParam("id") @NonNull Long id){
        //判断用户是否被禁言
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            log.info(ResultCode.FAILED_USER_BANNED.getMessage());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //帖子是否已删除
        Article article = articleService.selectById(id);
        if(article == null || article.getDeleteState() == 1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //用户是否是作者
        if(article.getUserId() != user.getId()){
            log.info(ResultCode.FAILED_UNAUTHORIZED.getMessage());
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        articleService.deleteById(id);
        return AppResult.success();
    }

    @ApiOperation("获取用户帖子列表")
    @GetMapping("/getAllByUserId")
    public AppResult<List<Article>> getAllByUserId(HttpServletRequest request,
                                                   @ApiParam("用户id")
                                                       @RequestParam(value = "userId", required = false)
                                                   Long userId){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(userId == null){
            userId = user.getId();
        }
        List<Article> result = articleService.selectByUserId(userId);
        return AppResult.success(result);
    }

}
